var ctx=window.daijia.ctx;
var lat=window.daijia.lat;
var lng=window.daijia.lng;
var map = null; //地图对象
var updateMap = true;

$(document).ready(function(){
	    //省、市、县
		new PCAS('provincial','city','county');
		
		$("#provincial").change(function(){
			if (updateMap) {
				initMap($(this).val());
			}
			$("#address").val("");
		});
		$("#city").change(function(){
			if (updateMap) {
				initMap($(this).val());
			}
			$("#address").val("");
		});
		$("#county").change(function(){
			if (updateMap) {
				initMap($(this).val());
			}
			$("#address").val("");
		});
		$(document).on("click","#btn_address",function(){
			if ($("#address").val() != null && $("#address").val().trim() != "") {
				updateMap = true;
				
				map = new BMap.Map("allmap");          
				map.centerAndZoom($("#city").val().trim(), 13);
				map.enableScrollWheelZoom();
				var local = new BMap.LocalSearch(map, {
					renderOptions:{map: map}
				});
				
				local.search($("#address").val().trim());
				
				local.setInfoHtmlSetCallback(function(poi,html){
					
					var point = new BMap.Point(poi.marker.point.lng,poi.marker.point.lat);
					
					var geoc = new BMap.Geocoder();
					
					geoc.getLocation(point, function(rs){
						
						var addComp = rs.addressComponents;
						$("#provincial option").each(function(index,element){
							if($(element).attr("value")==addComp.province){
								$(element).attr("selected","selected");
								updateMap=false;
								$(element).change();
								return false;
							}
						});
						
						$("#city option").each(function(index,element){
							if($(element).attr("value")==addComp.city){
								$(element).attr("selected","selected");
								updateMap=false;
								$(element).change();
								return false;
							}
						});
						
						$("#county option").each(function(index,element){
							if($(element).attr("value") == addComp.district){
						 		$(element).attr("selected","selected");
						 		updateMap=false;
						 		$(element).change();
						 		return false;
						 	}
						});
						
						$("#lat_label").text(poi.marker.point.lat);
						$("#lng_label").text(poi.marker.point.lng);
						$("#lat").val(poi.marker.point.lat);
						$("#lng").val(poi.marker.point.lng);
						var title = addComp.street;
						if (title != null && title !="") {
							$("#address").val(title);
						}
						
					});      
					
					
					var markers = map.getOverlays();
					for(var i=0;i<markers.length;i++){
						markers[i].show();
					}
				});
				var contextMenu = new BMap.ContextMenu();    
				var txtMenuItem =[{
					  text:'选择地点',
					  callback:function(p){
						    map.clearOverlays();				   		
							var memberMarker = new BMap.Marker(p);  // 创建标注
							map.addOverlay(memberMarker);
							var gc = new BMap.Geocoder();
							gc.getLocation(p, function(rs){
									var addComp = rs.addressComponents;
									$("#provincial option").each(function(index,element){
										if($(element).attr("value")==addComp.province){
											$(element).attr("selected","selected");
											updateMap=false;
											$(element).change();
											return false;
										}
									});
									$("#city option").each(function(index,element){
										if($(element).attr("value")==addComp.city){
											$(element).attr("selected","selected");
											updateMap=false;
											$(element).change();
											return false;
										}
									});
									$("#county option").each(function(index,element){
										if($(element).attr("value") == addComp.district){
									 		$(element).attr("selected","selected");
									 		updateMap=false;
									 		$(element).change();
									 		return false;
									 	}
									});
									
									$("#lat_label").text(p.lat);
									$("#lng_label").text(p.lng);
									$("#lat").val(p.lat);
									$("#lng").val(p.lng);
									var title = addComp.street;
									if (title != null && title !="") {
										$("#address").val(title);
									}
							});     
								
					   }
				}]
				
				for(var i=0; i < txtMenuItem.length; i++){
					contextMenu.addItem(new BMap.MenuItem(txtMenuItem[i].text,txtMenuItem[i].callback,100));
					if(i==1 || i==3) {
						contextMenu.addSeparator();
					}
				};
				map.addContextMenu(contextMenu);
			
			} else {
				$.scojs_message("请填写详细地址再定位", $.scojs_message.TYPE_ERROR);
			}
		});
		$(document).on("click","#create",function(){
			if ($("#name").val() == null || $("#name").val() == "") {
				$.scojs_message("加油站名称不能为空", $.scojs_message.TYPE_ERROR);
				return false;
			}
			if ($("#brand").val() == null || $("#brand").val() == "") {
				$.scojs_message("代理品牌不能为空", $.scojs_message.TYPE_ERROR);
				return false;
			}
			if ($("#companyId").val() == null) {
				$.scojs_message("请选择公司", $.scojs_message.TYPE_ERROR);
				return false;
			}
			if ($("#contacts").val() == null || $("#contacts").val() == "") {
				$.scojs_message("联系人不能为空", $.scojs_message.TYPE_ERROR);
				return false;
			}
			if ($("#contactsPhone").val() == null || $("#contactsPhone").val() == "") {
				$.scojs_message("联系人电话不能为空", $.scojs_message.TYPE_ERROR);
				return false;
			}
			if(!(/^[0-9]{7,14}$/.test($("#contactsPhone").val()))){
				$.scojs_message("联系人电话格式不正确", $.scojs_message.TYPE_ERROR);
				return false;
			}
			if($("#telephone").val().trim() != null && $("#telephone").val().trim() != "" && !(/^[0-9]{7,12}$/.test($("#telephone").val()))){
				$.scojs_message("座机号码格式不正确", $.scojs_message.TYPE_ERROR);
				return false;
			}
			
			if ($("#address").val() == null || $("#address").val() == "") {
				$.scojs_message("详细地址不能为空", $.scojs_message.TYPE_ERROR);
				return false;
			}
			if ($("#lat").val() == null || $("#lat").val() == "" || $("#lng").val() == null || $("#lng").val() == "") {
				$.scojs_message("地址不详细请重新定位", $.scojs_message.TYPE_ERROR);
				return false;
			}
			$("#companyName").val($("#companyId").find("option:selected").text());
			$("#create").attr("disabled","disabled");
			$.post(ctx+"/gasstation/create",$("#submit_form").serialize(),
			  function(data){
				if(data.success){
					$.scojs_message("创建成功", $.scojs_message.TYPE_OK);
					setTimeout(function(){
						window.location=ctx+"/gasstation/list";
					}, 800);
				}else{
					$("#create").removeAttr("disabled");
					$.scojs_message(data.message, $.scojs_message.TYPE_ERROR);
				}
			},'json');
		});
		$("#companyId").change(function(){
	    	var companyId=$("#companyId").val();
	    	$("#companyName").val($('#companyId').find("option:selected").text());
		});
});

	
function initMap(address) {
	updateMap = true;
	map = new BMap.Map("allmap"); 
	map.centerAndZoom(address, 13);
    map.enableScrollWheelZoom();
    var contextMenu = new BMap.ContextMenu();    
	var txtMenuItem =[{
		  text:'选择地点',
		  callback:function(p){
			    map.clearOverlays();				   		
				var memberMarker = new BMap.Marker(p);  // 创建标注
				map.addOverlay(memberMarker);
				var gc = new BMap.Geocoder();
				gc.getLocation(p, function(rs){
					var province=rs.addressComponents.province;
					var city = rs.addressComponents.city;
					var district = rs.addressComponents.district;
					
					if (province != null && province.trim() != "") {
						$("#provincial option").each(function(index,element){
						  	if($(element).attr("value")==province){
						 		$(element).attr("selected","selected");
						 		updateMap=false;
						 		$(element).change();
						 		return false;
						 	}
						});
					}
					if (city != null && city.trim() != "") {
						$("#city option").each(function(index,element){
						  	if($(element).attr("value") == city){
						 		$(element).attr("selected","selected");
						 		updateMap=false;
						 		$(element).change();
						 		return false;
						 	}
						});
					}
					if (district != null && district.trim() != "") {
						$("#county option").each(function(index,element){
							if($(element).attr("value") == district){
								$(element).attr("selected","selected");
								updateMap=false;
								$(element).change();
								return false;
							}
						});
					}	
					
					var addComp = rs.addressComponents;
					$("#lat_label").text(p.lat);
					$("#lng_label").text(p.lng);
					$("#lat").val(p.lat);
					$("#lng").val(p.lng);
					
					$("#address").val(addComp.street);
				});         
		   }
	}]
	
	for(var i=0; i < txtMenuItem.length; i++){
		contextMenu.addItem(new BMap.MenuItem(txtMenuItem[i].text,txtMenuItem[i].callback,100));
		if(i==1 || i==3) {
			contextMenu.addSeparator();
		}
	};
	map.addContextMenu(contextMenu);
	
}
		
	
